#include "DiscardStrategyCollection.h"
#include "Query.h"
#include <vector>

using namespace std;

int DiscardStrategyCollection::pickDiscard(CCardStack* hand, bool lastCardPicked, const GameState* state) {
    int voteCount[4] = {0};
    int voteOf[numStrategies];
    for (int i = 0; i < numStrategies; i++) {
        voteOf[i] = collection[i]->pickDiscard(hand, lastCardPicked, state);
        voteCount[voteOf[i]] += collection[i]->getDecisionCertainty() * collection[i]->getWeight();
    }
    int decision = Query::maxValueIndex(voteCount, 4);
    vector<int> tiedCards;
    for (int i = 0; i < 4; i++) {
        if (voteCount[i] == voteCount[decision]) {
            tiedCards.push_back(i);
        }
    }
    if (tiedCards.size() > 1) {
        int maxWeight = 0;
        for (unsigned int i = 0; i < tiedCards.size(); i++) {
            int possibleCard = tiedCards.at(i);
            for (int j = 0; j < numStrategies; j++) {
                int strategyWeight = collection[j]->getWeight();
                if (voteOf[j] == possibleCard && strategyWeight > maxWeight) {
                    maxWeight = strategyWeight;
                    decision = possibleCard;
                }
            }
        }
    }
    return decision;
}

int DiscardStrategyCollection::getDecisionCertainty() {
    return 0;
}

void DiscardStrategyCollection::opponentMove(int opponent, const Move& move) {
    for (int i = 0; i < numStrategies; i++) {
        collection[i]->opponentMove(opponent, move);
    }
}
